﻿using DevExpress.Web.ASPxTreeList;
using RR;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected RegistRar regrar = new RegistRar();

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
            if(cbb_role.Value !=null)
            { 
                TreeList.DataSourceID = "ds_menu";
                TreeList.DataBind();
                TreeList.ExpandAll();
            }
            else
            {TreeList.DataSourceID = "";}
       // }
    }

    protected void cbb_role_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateTreeList();
    }

    void CreateTreeList()
    {
        int role = Convert.ToInt32(cbb_role.Value);
        var query = (from m in regrar.MENURIGHT
                     where m.WORKERSTATE == role
                     select m);

        TreeList.UnselectAll();

        foreach (var item in query)
        {
            TreeListNode node = TreeList.FindNodeByFieldValue("ID", item.MENUID);
            if (node != null && !node.HasChildren)
                node.Selected = true;
        }
    }

    public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
    {
        DataTable dtReturn = new DataTable();

        // column names 
        PropertyInfo[] oProps = null;

        if (varlist == null) return dtReturn;

        foreach (T rec in varlist)
        {


            if (oProps == null)
            {
                oProps = ((Type)rec.GetType()).GetProperties();
                foreach (PropertyInfo pi in oProps)
                {
                    Type colType = pi.PropertyType;

                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
                    == typeof(Nullable<>)))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }

                    dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                }
            }

            DataRow dr = dtReturn.NewRow();

            foreach (PropertyInfo pi in oProps)
            {
                dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                (rec, null);
            }

            dtReturn.Rows.Add(dr);
        }
        return dtReturn;
    }


    protected void TreeList_SelectionChanged(object sender, EventArgs e)
    {
        bt_save.Enabled = true;
    }
    protected void bt_save_Click(object sender, EventArgs e)
    {
        SaveNodes();
    }

    private void SaveNodes()
    {
        int workerstate = Convert.ToInt32(cbb_role.Value);
        int parent = -1;
        foreach (var node in TreeList.GetAllNodes())
        {
            if (node.Selected == true && node.ParentNode.Selected == false)
            {
                int temp = Convert.ToInt32(node.ParentNode.GetValue("ID"));
                if (temp != parent)
                    MENURIGHT.SetMenuRight(regrar, workerstate, temp, node.Selected);
                parent = temp;
            }

            int id = Convert.ToInt32(node.GetValue("ID"));
            if (id != parent)
                MENURIGHT.SetMenuRight(regrar, workerstate, id, node.Selected);
        }

        regrar.SaveChanges();
    }
    protected void cbPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        switch (e.Parameter)
        {
            case "clear": lbl_info.Text = "";
                bt_save.Enabled = true;
                break;
            case "saved":
                SaveNodes();
                bt_save.Enabled = false;
                lbl_info.Text = "Информация сохранена.";                
                break;
        }
    }
}